import { logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken, getSid, setSid, removeSid, getOpenID, setOpenID, removeOpenID } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    sid: getSid(),
    uid: "",
    name: '',
    avatar: '',
    unlock_chart: 0,
    unlock_end_time: 0,
    user_type: 0,
    status: 0,
    openid: getOpenID(),
    roles: [],
    showLogin: false,
    is_vip: 0,
    vCount: 0,
    vip_last_day: 0,
    balance: '',
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_OPENID: (state, openid) => {
      state.openid = openid
    },
    SET_SID: (state, sid) => {
      state.sid = sid
    },
    SET_UID: (state, uid) => {
      state.uid = uid
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_USER_TYPE: (state, user_type) => {
      state.user_type = user_type
    },
    SET_UNLOCK_LEVEL: (state, unlock_chart) => {
      state.unlock_chart = unlock_chart
    },
    SET_UNLOCK_END_TIME: (state, unlock_end_time) => {
      state.unlock_end_time = unlock_end_time
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_SHOW: (state, showLogin) => {
      state.showLogin = showLogin
    },
    SET_VIP: (state, is_vip) => {
      state.is_vip = is_vip
    },
    SET_COUNT: (state, vCount) => {
      state.vCount = vCount
    },
    SET_LAST: (state, vip_last_day) => {
      state.vip_last_day = vip_last_day
    },
    SET_BANLANCE: (state, balance) => {
      state.balance = balance
    },
  },

  actions: {
    // 登录
    Login({ commit }, loginInfo) {
      return new Promise((resolve, reject) => {
        commit('SET_SID', loginInfo.token)
        commit('SET_TOKEN', loginInfo.token)
        // commit('SET_OPENID', loginInfo.openid)
        // 设置token
        setToken(loginInfo.token)
        setSid(loginInfo.token)
        // setOpenID(loginInfo.openid)
        // 跳转页面
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      //      console.log('get user info')
      //      console.log(state)
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          if (response.code == 2) {
            this.$store.dispatch('FedLogOut').then(() => {
              next({ path: '/' })
            })
          }
          const data = response.data
          console.log(response.data)
          // // 设置用户信息
          commit('SET_USER_TYPE', data.user_type)
          commit('SET_UNLOCK_LEVEL', data.unlock_chart)
          commit('SET_UNLOCK_END_TIME', data.unlock_end_time)
          commit('SET_NAME', data.nickname)
          commit('SET_AVATAR', data.avatar)
          commit('SET_UID', data.id)
          commit('SET_VIP', data.is_vip)
          commit('SET_COUNT', data.vCount)
          commit('SET_LAST', data.vip_last_day)
          commit('SET_BANLANCE', data.balance)

          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_SID', '')
          commit('SET_UID', '')
          commit('SET_USER_TYPE', '')
          commit('SET_UNLOCK_LEVEL', '')
          commit('SET_UNLOCK_END_TIME', '')
          commit('SET_NAME', '')
          commit('SET_AVATAR', '')
          removeToken()
          removeSid()
          removeOpenID()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_SID', '')
        commit('SET_UID', '')
        commit('SET_USER_TYPE', '')
        commit('SET_UNLOCK_LEVEL', '')
        commit('SET_UNLOCK_END_TIME', '')
        commit('SET_NAME', '')
        commit('SET_AVATAR', '')
        removeToken()
        removeSid()
        removeOpenID()
        resolve()
      })
    },
    //没有登录时，打开登录弹窗
    toLogin({ commit }) {
      commit('SET_SHOW', true)
    }
  }
}

export default user
